package leetcode20211011;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: LUYAO
 * Date: 2021-10-11
 * Time: 22:15
 */
public class SearchAndBack {
}

//翻转单词顺序
class Solution1 {
    public String reverseWords(String s) {
        s = s.trim();
        StringBuilder res = new StringBuilder();
        int j = s.length()-1;
        int i = j;
        while(i >= 0){
            while(i >= 0 && s.charAt(i) != ' ') i--;
            res.append(s.substring(i+1,j+1)+" ");
            while(i >= 0 && s.charAt(i) == ' ') i--;
            j = i;
        }

        return res.toString().trim();

    }
}
